"""Functions to draw graphs etc."""

#_______________________________________________________________________________

import difflib
import logging
import platform

import matplotlib.pyplot as plt
import networkx as nx

from _debug import logged

#_______________________________________________________________________________

_logger = logging.getLogger('nxchaos.util')

#_______________________________________________________________________________

def render_picture(graph, to_file=True, show=False, path="temp.png", pos=None, with_labels=True, **kwargs):

    nx.draw_networkx(graph, pos, with_labels, **kwargs)
    
    if to_file:
        plt.savefig(path)
        
    if show:
        plt.show()
        
    plt.close()

def render_text(graph, to_file=True, path='temp.txt'):

    if to_file:
        nx.write_adjlist(graph, path)

def compare_results(file1, file2, save_to_file=False, path="diff.txt"):

    f1 = open(file1, 'r')
    f2 = open(file2, 'r')

    diffInstance = difflib.Differ()
    diffList = list(diffInstance.compare(f1.readlines(), f2.readlines()))

    f1.close()
    f2.close()

    # not so optimal...
    if save_to_file:
        with open(path, "w+") as f:
            for line in diffList:
                f.write(line)

    for line in diffList:
        if line[0] != ' ':
            return False, diffList

    return True, diffList
